package com.hrh.selectsystem.controller;

import cn.hutool.db.PageResult;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hrh.selectsystem.aop.MyJwtInterceptor;
import com.hrh.selectsystem.common.BaseResponse;
import com.hrh.selectsystem.common.DeleteRequest;
import com.hrh.selectsystem.common.ErrorCode;
import com.hrh.selectsystem.common.ResultUtils;
import com.hrh.selectsystem.exception.BusinessException;
import com.hrh.selectsystem.exception.ThrowUtils;
import com.hrh.selectsystem.model.dto.admin.AdminLoginRequest;
import com.hrh.selectsystem.model.dto.admin.AdminUpdateRequest;
import com.hrh.selectsystem.model.dto.choicerecord.ChoiceRecordQueryRequest;
import com.hrh.selectsystem.model.dto.file.MentorExcelImport;
import com.hrh.selectsystem.model.dto.file.StudentExcelImport;
import com.hrh.selectsystem.model.dto.matchResult.MatchRecordQueryRequest;
import com.hrh.selectsystem.model.dto.mentor.MentorAddRequest;
import com.hrh.selectsystem.model.dto.mentor.MentorQueryRequest;
import com.hrh.selectsystem.model.dto.mentor.MentorUpdateRequest;
import com.hrh.selectsystem.model.dto.student.StudentAddRequest;
import com.hrh.selectsystem.model.dto.student.StudentQueryRequest;
import com.hrh.selectsystem.model.dto.student.StudentUpdateRequest;
import com.hrh.selectsystem.model.entity.*;
import com.hrh.selectsystem.model.vo.MatchRecordQueryVO;
import com.hrh.selectsystem.model.vo.MatchVO;
import com.hrh.selectsystem.model.vo.RoundVO;
import com.hrh.selectsystem.model.vo.admin.AdminByTokenVO;
import com.hrh.selectsystem.model.vo.admin.AdminVO;
import com.hrh.selectsystem.model.vo.admin.LoginAdminVO;
import com.hrh.selectsystem.model.vo.choiceRecord.ChoiceRecordQueryVO;
import com.hrh.selectsystem.service.*;
import com.hrh.selectsystem.utils.JwtTokenUtils;
import com.hrh.selectsystem.utils.doexcel.MentorDataListener;
import com.hrh.selectsystem.utils.doexcel.StudentDataListener;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.net.http.HttpHeaders;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.hrh.selectsystem.service.impl.StudentServiceImpl.SALT;

/**
 * 管理员接口
 * @author hrh
 */
@RestController
@RequestMapping("/admin")
@Api(tags = "管理员接口")
@Slf4j
public class AdminController {

    @Resource
    private AdminService adminService;
    @Resource
    private StudentService studentService;
    @Resource
    private MentorService mentorService;
    @Resource
    private RoundService roundService;
    @Resource
    private ChoiceRecordService choiceRecordService;
    @Resource
    private MatchResultService matchResultService;
    @Resource
    private MentorApplicationCountService mentorApplicationCountService;

    /**
     * 管理员登录
     *
     * @param adminLoginRequest
     * @param request
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "管理员登录")
    public BaseResponse<LoginAdminVO> adminLogin(@RequestBody AdminLoginRequest adminLoginRequest, HttpServletRequest request) {
        if (adminLoginRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String adminName = adminLoginRequest.getAdminname();
        String adminPassword = adminLoginRequest.getAdminpassword();
        if (StringUtils.isAnyBlank(adminName, adminPassword)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        LoginAdminVO loginAdminVO = adminService.adminLogin(adminName, adminPassword, request);
        String userrole = "admin";
        //登录成功后，生成jwt令牌
        String token = JwtTokenUtils.getToken(
                loginAdminVO.getId().toString(),
                adminPassword,
                userrole
        );
        loginAdminVO.setToken(token);
        return ResultUtils.success(loginAdminVO);
    }

    /**
     * 根据token获取管理员信息
     * @param request
     * @return
     */
    @GetMapping("/token")
    @ApiOperation(value = "根据token获取管理员信息(无role)")
    public BaseResponse<AdminByTokenVO> getTokenAdmin(HttpServletRequest request) {
        String token = request.getHeader("token");
        String userId = MyJwtInterceptor.getUserIdFromToken(token);
        Admin admin = adminService.getById(userId);
        if (admin == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户不存在");
        }
        AdminByTokenVO adminByTokenVO = new AdminByTokenVO();
        BeanUtils.copyProperties(admin, adminByTokenVO);
        return ResultUtils.success(adminByTokenVO);
    }

    /**
     * 获取当前登录管理员信息
     *
     * @param request
     * @return
     */
    @GetMapping("/get-user")
    @ApiOperation(value = "获取当前登录管理员信息")
    public BaseResponse<AdminVO> getAdminById(HttpServletRequest request) {
        Admin admin = adminService.getLoginAdmin(request);
        AdminVO adminVO = adminService.getAdminVO(admin);
        return ResultUtils.success(adminVO);
    }

    /**
     * 管理员注销
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation(value = "管理员注销")
    public BaseResponse<Boolean> adminLogout(HttpServletRequest request) {
        if (request == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = adminService.adminLogout(request);
        return ResultUtils.success(result);
    }

    /**
     * 管理员更新个人信息
     *
     * @param adminUpdateRequest
     * @param request
     * @return
     */
    @PostMapping("/update-password")
    @ApiOperation(value = "管理员修改个人密码")
    public BaseResponse<Boolean> updateAdmin(@RequestBody AdminUpdateRequest adminUpdateRequest,
                                              HttpServletRequest request) {
        if (adminUpdateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Admin loginAdmin = adminService.getLoginAdmin(request);
        Admin admin = new Admin();
        BeanUtils.copyProperties(adminUpdateRequest, admin);
        admin.setId(loginAdmin.getId());
        boolean result = adminService.updateById(admin);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 管理员创建学生
     *
     * @param studentAddRequest
     * @param request
     * @return
     */
    @PostMapping("/student/add")
    @ApiOperation(value = "管理员创建学生")
    public BaseResponse<Long> addStudent(@RequestBody StudentAddRequest studentAddRequest, HttpServletRequest request) {
        if (studentAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Student student = new Student();
        BeanUtils.copyProperties(studentAddRequest, student);
        // 默认密码学号
        String defaultPassword = studentAddRequest.getStudentaccount();
        // 默认角色学生
        String userRole = "student";
        String encryptPassword = DigestUtils.md5DigestAsHex((SALT + defaultPassword).getBytes());
        student.setStudentpassword(encryptPassword);
        student.setUserrole(userRole);
        boolean result = studentService.save(student);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(student.getId());
    }

    /**
     * 管理员从excel导入学生
     * @param file
     * @return
     */
    @PostMapping("/student/import-students")
    @ApiOperation(value = "管理员从excel批量导入学生")
    public BaseResponse<String> importStudents(@RequestParam("file") MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), StudentExcelImport.class, new StudentDataListener(studentService)).sheet().doRead();
            return ResultUtils.success("导入成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "导入失败");
        }
    }

    /**
     * 管理员创建导师
     *
     * @param mentorAddRequest
     * @param request
     * @return
     */
    @PostMapping("/mentor/add")
    @ApiOperation(value = "管理员创建导师")
    public BaseResponse<Long> addMentor(@RequestBody MentorAddRequest mentorAddRequest, HttpServletRequest request) {
        if (mentorAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Mentor mentor = new Mentor();
        BeanUtils.copyProperties(mentorAddRequest, mentor);
        // 默认密码 工号
        String defaultPassword = mentorAddRequest.getMentoraccount();
        // 默认角色导师
        String userRole = "mentor";
        String encryptPassword = DigestUtils.md5DigestAsHex((SALT + defaultPassword).getBytes());
        mentor.setMentorpassword(encryptPassword);
        mentor.setUserrole(userRole);
        boolean result = mentorService.save(mentor);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(mentor.getId());
    }

    /**
     * 管理员从excel导入导师
     * @param file
     * @return
     */
    @PostMapping("/mentor/import-mentors")
    @ApiOperation(value = "管理员从excel批量导入导师")
    public BaseResponse<String> importMentors(@RequestParam("file") MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), MentorExcelImport.class, new MentorDataListener(mentorService)).sheet().doRead();
            return ResultUtils.success("导入成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "导入失败");
        }
    }

    /**
     * 管理员删除学生
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/student/delete")
    @ApiOperation(value = "管理员删除学生")
    public BaseResponse<Boolean> deleteStudent(@RequestBody List<DeleteRequest> deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = false;

        for (DeleteRequest deleteRequest1 : deleteRequest) {
            Long id = deleteRequest1.getId();
            //删除学生之前先删除学生志愿记录
            choiceRecordService.remove(new QueryWrapper<ChoiceRecord>().eq("studentid", id));
            //删除学生的双选结果
            matchResultService.remove(new QueryWrapper<MatchResult>().eq("studentid", id));
            result = studentService.removeById(id);
        }
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(result);
    }

    /**
     * 管理员删除导师
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/mentor/delete")
    @ApiOperation(value = "管理员删除导师")
    public BaseResponse<Boolean> deleteMentor(@RequestBody List<DeleteRequest> deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = false;

        for (DeleteRequest deleteRequest1 : deleteRequest) {
            Long id = deleteRequest1.getId();
            choiceRecordService.remove(new QueryWrapper<ChoiceRecord>().eq("firstchoice", id));
            choiceRecordService.remove(new QueryWrapper<ChoiceRecord>().eq("secondchoice", id));
            choiceRecordService.remove(new QueryWrapper<ChoiceRecord>().eq("thirdchoice", id));
            matchResultService.remove(new QueryWrapper<MatchResult>().eq("mentorid", id));
            mentorApplicationCountService.remove(new QueryWrapper<MentorApplicationCount>().eq("mentorid", id));
            //导师申请数表删除
            result = mentorService.removeById(id);
        }
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(result);
    }

    /**
     * 管理员更新学生
     *
     * @param studentUpdateRequest
     * @param request
     * @return
     */
    @PostMapping("/student/update")
    @ApiOperation(value = "管理员更新学生")
    public BaseResponse<Boolean> updateStudent(@RequestBody StudentUpdateRequest studentUpdateRequest,
                                            HttpServletRequest request) {
        if (studentUpdateRequest == null || studentUpdateRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Student student = studentService.getById(studentUpdateRequest.getId());
        if (student == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        //取出新密码
        String newPassword = studentUpdateRequest.getStudentpassword();
        if (newPassword != null) {
            if (newPassword.length() < 6) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR, "新密码长度小于6位，不符合规范");
            }
            String encryptPassword = DigestUtils.md5DigestAsHex((SALT + newPassword).getBytes());
            student.setStudentpassword(encryptPassword);
        }
        student = studentService.notNullStudent(studentUpdateRequest, student);
        boolean result = studentService.updateById(student);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 管理员更新导师
     *
     * @param mentorUpdateRequest
     * @param request
     * @return
     */
    @PostMapping("/mentor/update")
    @ApiOperation(value = "管理员更新导师")
    public BaseResponse<Boolean> updateMentor(@RequestBody MentorUpdateRequest mentorUpdateRequest,
                                               HttpServletRequest request) {
        if (mentorUpdateRequest == null || mentorUpdateRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Mentor mentor = mentorService.getById(mentorUpdateRequest.getId());
        if (mentor == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String newPassword = mentorUpdateRequest.getMentorpassword();
        if (newPassword != null) {
            if (newPassword.length() < 6) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR, "新密码长度小于6位，不符合规范");
            }
            String encryptPassword = DigestUtils.md5DigestAsHex((SALT + newPassword).getBytes());
            mentor.setMentorpassword(encryptPassword);
        }
        mentor = mentorService.notNullMentor(mentorUpdateRequest, mentor);
        boolean result = mentorService.updateById(mentor);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 管理员根据 id 获取学生
     *
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/student/get")
    @ApiOperation(value = "管理员根据 id 获取学生")
    public BaseResponse<Student> getStudentById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Student student = studentService.getById(id);
        ThrowUtils.throwIf(student == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(student);
    }

    /**
     * 管理员根据 id 获取导师
     *
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/mentor/get")
    @ApiOperation(value = "管理员根据 id 获取导师")
    public BaseResponse<Mentor> getMentorById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Mentor mentor = mentorService.getById(id);
        ThrowUtils.throwIf(mentor == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(mentor);
    }

    /**
     * 管理员分页获取学生列表
     *
     * @param studentQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/student/list/page")
    @ApiOperation(value = "管理员分页获取学生列表")
    public BaseResponse<Page<Student>> listStudentByPage(@RequestBody StudentQueryRequest studentQueryRequest,
                                                         HttpServletRequest request) {
        long current = studentQueryRequest.getCurrent();
        long size = studentQueryRequest.getPageSize();
        Page<Student> studentPage = studentService.page(new Page<>(current, size),
                studentService.getQueryWrapper(studentQueryRequest));
        return ResultUtils.success(studentPage);
    }

    /**
     * 管理员分页获取导师列表
     *
     * @param mentorQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/mentor/list/page")
    @ApiOperation(value = "管理员分页获取导师列表")
    public BaseResponse<Page<Mentor>> listMentorByPage(@RequestBody MentorQueryRequest mentorQueryRequest,
                                                         HttpServletRequest request) {
        long current = mentorQueryRequest.getCurrent();
        long size = mentorQueryRequest.getPageSize();
        Page<Mentor> mentorPage = mentorService.page(new Page<>(current, size),
                mentorService.getQueryWrapper(mentorQueryRequest));
        return ResultUtils.success(mentorPage);
    }

    /**
     * 管理员禁用、启用学生账号
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/student/update-status")
    @ApiOperation(value = "管理员停用、启用学生账号")
    public BaseResponse<Boolean> updateStudentStatus(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Student student = studentService.getById(id);
        if (student == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        student.setStatus(student.getStatus() == 0 ? 1 : 0);
        boolean result = studentService.updateById(student);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 管理员禁用、启用导师账号
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/mentor/update-status")
    @ApiOperation(value = "管理员停用、启用导师账号")
    public BaseResponse<Boolean> updateMentorStatus(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Mentor mentor = mentorService.getById(id);
        if (mentor == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        mentor.setStatus(mentor.getStatus() == 0 ? 1 : 0);
        boolean result = mentorService.updateById(mentor);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 管理员分页获取轮次表
     * @param request
     * @return
     */
    @GetMapping("/round/list/page")
    @ApiOperation(value = "管理员分页获取轮次表")
    public BaseResponse<Page<RoundVO>> listRoundByPage(HttpServletRequest request) {
        long current = 1;
        long size = 3;
        return ResultUtils.success(roundService.listRoundByPage(current, size));
    }

    /**
     * 管理员结束当前轮次
     * @param request
     * @return
     */
    @GetMapping("/round/end")
    @ApiOperation(value = "管理员结束当前轮次")
    public BaseResponse<Boolean> endCurrentRound(HttpServletRequest request) {
        boolean result = roundService.endCurrentRound();
        return ResultUtils.success(result);
    }

    /**
     * 管理员启动下一轮次
     * @param request
     * @return
     */
    @GetMapping("/round/next")
    @ApiOperation(value = "管理员启动下一轮次")
    public BaseResponse<Boolean> startNextRound(HttpServletRequest request) {
        boolean result = roundService.startNextRound();
        return ResultUtils.success(result);
    }

    /**
     * 管理员重置所有轮次状态
     * @param request
     * @return
     */
    @GetMapping("/round/reset")
    @ApiOperation(value = "管理员重置所有轮次状态")
    public BaseResponse<Boolean> resetAllRounds(HttpServletRequest request) {
        boolean result = roundService.resetAllRounds();
        return ResultUtils.success(result);
    }

    /**
     * 管理员分页获取学生申请消息列表
     * @param choiceRecordQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/submit-choices/list/page")
    @ApiOperation(value = "管理员分页获取学生申请消息列表")
    public BaseResponse<Page<ChoiceRecordQueryVO>> listSubmitChoicesByPage(@RequestBody ChoiceRecordQueryRequest choiceRecordQueryRequest, HttpServletRequest request) {
        if (choiceRecordQueryRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long current = choiceRecordQueryRequest.getCurrent();
        long size = choiceRecordQueryRequest.getPageSize();
        return ResultUtils.success(choiceRecordService.listSubmitChoicesByPage(choiceRecordQueryRequest, current, size));
    }

    /**
     * 管理员分页获取学生导师双选成功列表
     */
    @PostMapping("/success-choices/list/page")
    @ApiOperation(value = "管理员分页获取学生导师双选成功列表")
    public BaseResponse<Page<MatchRecordQueryVO>> listSuccessChoicesByPage(@RequestBody MatchRecordQueryRequest matchRecordQueryRequest, HttpServletRequest request) {
        if (matchRecordQueryRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long current = matchRecordQueryRequest.getCurrent();
        long size = matchRecordQueryRequest.getPageSize();
        return ResultUtils.success(matchResultService.listMatchRecordByPage(matchRecordQueryRequest, current, size));
    }

    /**
     *  管理员分页查询有名额导师
     * @param mentorQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/mentor/list/page/has-quota")
    @ApiOperation(value = "管理员分页查询有名额导师")
    public BaseResponse<Page<Mentor>> listMentorByPageHasQuota(@RequestBody MentorQueryRequest mentorQueryRequest, HttpServletRequest request) {
//        List<Mentor> mentorList = mentorService.getMentorHasQuota(mentorQueryRequest);
//        Page<Mentor> mentorPage = new Page<>(
//                mentorQueryRequest.getCurrent(),
//                mentorQueryRequest.getPageSize()
//        );
//        mentorPage.setTotal(mentorList.size());
//        mentorPage.setRecords(mentorList);
//        return ResultUtils.success(mentorPage);
        long current = mentorQueryRequest.getCurrent();
        long size = mentorQueryRequest.getPageSize();
        Page<Mentor> mentorPage = mentorService.getMentorHasQuota(mentorQueryRequest, current, size);
        return ResultUtils.success(mentorPage);
    }

    /**
     * 管理员分页获取无导师学生
     * @param studentQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/student/list/page/no-mentor")
    @ApiOperation(value = "管理员分页获取无导师学生")
    public BaseResponse<Page<Student>> listStudentByPageNoMentor(@RequestBody StudentQueryRequest studentQueryRequest, HttpServletRequest request) {
        long current = studentQueryRequest.getCurrent();
        long size = studentQueryRequest.getPageSize();
        Page<Student> studentPage = studentService.getStudentNoMentor(studentQueryRequest, current, size);
        return ResultUtils.success(studentPage);
    }

    /**
     * 管理员强行匹配导师学生
     * @param studentId
     * @param mentorId
     * @param request
     * @return
     */
    @PostMapping("/match/force")
    @ApiOperation(value = "管理员强行匹配导师学生")
    public BaseResponse<Boolean> forceMatch(long studentId,long mentorId, HttpServletRequest request) {
        boolean result = matchResultService.forceMatch(studentId, mentorId);
        return ResultUtils.success(result);
    }

    /**
     * 管理员导出匹配成功的导师和学生信息
     * @param response
     * @return
     */
    @GetMapping("/match/export")
    @ApiOperation(value = "管理员导出匹配成功的导师和学生信息")
    public void exportMatchResult(HttpServletResponse response) throws IOException {
        // 设置响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        // 设置防止中文名乱码
        String filename = URLEncoder.encode("双选结果", "utf-8");
        // 文件下载方式(附件下载还是在当前浏览器打开)
        response.setHeader("Content-disposition", "attachment;filename=" +
                filename + ".xlsx");
        //查询所有匹配成功的导师和学生信息
        List<MatchRecordQueryVO> matchResults = matchResultService.listMatchRecordByPage(new MatchRecordQueryRequest(), 1, 1000).getRecords();
        //转化成MatchVO
        List<MatchVO> matchVOS = matchResults.stream().map(matchResult -> {
            MatchVO matchVO = new MatchVO();
            BeanUtils.copyProperties(matchResult, matchVO);
            return matchVO;
        }).collect(Collectors.toList());
        // 构建写入到excel文件的数据
        EasyExcel.write(response.getOutputStream(), MatchVO.class)
                .sheet("双选结果")
                .doWrite(matchVOS);
    }
}
